﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
using Sunboxsoft.ASMS.Client.Domain;

namespace Sunboxsoft.ASMS.Client.UI.Storage
{
    /// <summary>
    /// 库存查询
    /// </summary>
    public partial class LocalStorageView : UserControl, IView
    {
        //当前页  
        private int _pageIndex = 0;
        //总共几页
        private int _pageCount = 0;

        Promise _promise = new Promise();
        WebRef.VMaterial itemVMaterial = null;

        public LocalStorageView()
        {
            InitializeComponent();
            this.GCData.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_Click));
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    ctlLookUpEditStockType.BindIntCodeTextItem(Domain.Constant.StockQueryTypes.Enumerate(),
                        Domain.Constant.StockQueryTypes.AllItem,
                        true);
                    _promise.Resolve();
                }));
            })
            .Then(() =>
            {
                ctlSearchLookUpWareHouse.BindEWarehouse(_promise, null, true);
            })
            .Then(() =>
            {
                ctlSearchLookUpEditCatagory.BindEMasterDataItem(_promise, Domain.Constant.MasterDataCodes.MaterialCategory, null, null, false, true);
            })
            .Then(BindData)
            .Run();
        }
        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                if (args.ClosedArgs.ActiveArgs.TagCode == 0x01)
                {
                    itemVMaterial = args.ClosedArgs.Data as WebRef.VMaterial;
                    this.ctlBtnQryMaterial.Text = itemVMaterial.Name;
                }
                else
                {
                    BindData();
                }
            }
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("库存查询");
        }
        #endregion

        #region Method
        /// <summary>
        /// 数据列表绑定
        /// </summary>
        private void BindData()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                Guid? warehouseid = null, materialid = null;
                WebRef.VWarehouse itemEWarehouse = ctlSearchLookUpWareHouse.GetSelectedValue<WebRef.VWarehouse>();
                if (itemEWarehouse != null)
                {
                    warehouseid = itemEWarehouse.Id;
                }
                if (itemVMaterial != null)
                {
                    materialid = itemVMaterial.Id;
                }
                PagedResult<WebRef.VStock> result = new Service.QueryService().QueryStockPagedList(
                    txtQryFilter.Text.Trim(),
                    LookUpEditUtil.GetIntCode(this.ctlLookUpEditStockType),
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    warehouseid,
                    materialid,
                    ctlSearchLookUpEditCatagory.GetSelectedValue<WebRef.EMasterDataItem>().GetCode(),
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    this.HideMask();
                    if (result.IsSuccess)
                    {
                        this.GCData.Paginate<WebRef.VStock>(result, out _pageIndex, out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                }));
            });
        }
        /// <summary>
        /// 获取每页条数限制
        /// </summary>
        /// <returns></returns>
        private int GetPageLimit()
        {
            int pagesize = 25;
            return pagesize;
        }
        #endregion

        #region Action
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, object e)
        {
            _pageIndex = 0;
            BindData();
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            BindData();
        }
        /// <summary>
        /// 清除所选商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnQryMaterial_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.Equals("clear"))
            {
                this.ctlBtnQryMaterial.Text = "";
            }
            else
            {
                UIContext.Current.ViewManager.PopupView(new Pickers.MaterialPicker(Domain.Constant.CommonStatus.Enabled),
                   new PopupViewArgs("选择商品") { TagCode = 0x01 });
            }
        }
        #endregion
    }
}
